package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
//import org.springframework.data.domain.Page;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {
    /**
     * 用户下单
     *
     * */
    void insert(Orders orders);

    void update(Orders orders);

    @Select("select * from orders where status = #{status} and order_time < #{time}")
    List<Orders> getStatusByTimeout(Integer status, LocalDateTime time);


    @Select("select * from orders where status = #{status}")
    List<Orders> getStatusIsFour(Integer deliveryInProgress);
    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 根据订单id查询订单
     * */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);
    /**
     * 根据用户id查询所有订单
     * */
    @Select("select * from orders where user_id = #{userId} order by order_time desc")
    List<Orders> getByUserId(Long userId);

    @Delete("delete from orders where id = #{id}")
    void deleteById(Long id);

    /**
     * 分页条件查询并按下单时间排序
     * @param ordersPageQueryDTO
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据状态统计订单数据
     * */
    @Select("select COUNT(id) from orders where status = #{status}")
    Integer statusCount(Integer status);

    /**
     * 统计指定时间段内下单并且订单完成的用户数量
     * */
    @Select("select count(distinct id) from orders where status=#{status} and order_time between #{begin} and #{end} ")
    Integer getUserAccountByStatusAndDate(Integer status, LocalDate begin, LocalDate end);
}

